<?php $archivos=strtoupper($fondo->getSiglasinst())?>
<?php
header ("Content-Type: application/force-download");
header("Pragma: ");
header('Cache-control: ');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
//header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename='Presupuesto_$archivos.ods'");
?>    
<html>
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <?php
$fi = explode("-", $proyecto->getPeriodos()->getFechai());
$ff = explode("-", $proyecto->getPeriodos()->getFechaf());
$ainicial = $fi[0];
$afinal = $ff[0];
$minicial = $fi[1];
$mfinal = $ff[1];
?>

<div id="cajaprincipalproyecto" align="center">

    <div id="cajapresupuestos" align="center">
        <div align="left"><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<b>SECRETARÍA NACIONAL DE EDUCACIÓN SUPERIOR, CIENCIA, TECNOLOGÍA E INNOVACIÓN</b></br></div>
        <div align="left"><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<b>SUBSECRETARÍA DE INVESTIGACIÓN CIENTÍFICA</b><br></div>
        <div align="left"><br>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<b>DIRECCIÓN DE MONITOREO, SEGUIMIENTO Y EVALUACIÓN DE PROGRAMAS Y PROYECTOS DE INVESTIGACIÓN CIENTÍFICA</b></br></div>
        <div align="left"><br><b>Código:</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<?php echo $proyecto->getCodigo() ?></br></div>
        <div align="left"><br><b>Proyecto:</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<?php echo $proyecto->getNombre() ?></br></div>
        <div align="left"><br><b>Director:</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp;<?php echo $proyecto->getDirector() ?></br></div>
        <div align="left"><br><b>Institución:</b>&nbsp&nbsp;<?php echo $fondo->getNInst() ?></br></div><br><br>
        <table>
            <tr>
                <td style="background-color: #fdcf16;"><B>PRESUPUESTO MENSUAL POR LÍNEAS DE FINANCIAMIENTO, ENTIDAD <?php echo strtoupper($fondo->getSiglasInst()) ?></B></td>
            </tr>
        </table><br>
        <div align="center" >
            <table  border="1" class="table"> 
                <tr>
                    <td><td><?php include_partial('tdanio'); ?></td></td>
                </tr>
                <?php
                $c3 = 0;
                foreach ($techospresupuestarioss as $tchos):    
                    $c3++;
                    ?>         
                    <tr>
                        <td style="background-color: #f33" ><b>&nbsp;<?php echo $c3 ?>&nbsp;<?php echo $tchos->getNombre() ?></b></td>                        
                    </tr>  
                    <?php
                    $descripc = Doctrine_Core::getTable('descripciongeneral')->createQuery('d')
                            ->where('d.id_tp = ?', $tchos->getIdTp())
                            ->execute();

                    foreach ($descripc as $des) :
                        $sumades = 0;
                        ?>
                        <tr>
                            <td ><?php echo $des->getNombre() ?></td>
                            <td ><div id="cajacodigo"><?php echo $des->getCodigo() ?></div></td>
                            <?php
                            if ($ainicial == $afinal) {
                                for ($i = $minicial; $i <= $mfinal; $i++) {
                                    $f = Doctrine_Query::create()
                                            ->from('valor v')
                                            ->where('v.id_dg = ?', $des->getIdDg())
                                            ->andwhere('v.mes = ?', $i)
                                            ->andwhere('v.anio = ?', $ainicial)
                                            ->andwhere('v.id_fo = ?', $fondo->getIdFo());
                                    $valo = $f->fetchOne();
                                    if (!empty($valo)) {
                                        $sumades = $sumades + $valo->getVal();
                                        ?>
                                        <td valign="baseline" align="center"><?php echo $valo->getVal(); ?></td>
                                    <?php } else { ?>
                                        <td valign="baseline" align="center">0.00</td>
                                        <?php
                                    }
                                }
                            } else {
                                //////////
                                if ($ainicial < $afinal) {
                                    $anio = 0;
                                    $anio = $afinal - $ainicial;
                                    if ($anio == 1) {
                                        for ($i = $minicial; $i <= 12; $i++) {
                                            $f = Doctrine_Query::create()
                                                    ->from('valor v')
                                                    ->where('v.id_dg = ?', $des->getIdDg())
                                                    ->andwhere('v.mes = ?', $i)
                                                    ->andwhere('v.anio = ?', $ainicial)
                                                    ->andwhere('v.id_fo = ?', $fondo->getIdFo());
                                            $valo = $f->fetchOne();
                                            if (!empty($valo)) {
                                                $sumades = $sumades + $valo->getVal();
                                                ?>
                                                <td valign="baseline" align="center"><?php echo $valo->getVal(); ?></td>
                                            <?php } else { ?>
                                                <td valign="baseline" align="center">0.00</td>
                                                <?php
                                            }
                                        }

                                        for ($i = 1; $i <= $mfinal; $i++) {
                                            $f = Doctrine_Query::create()
                                                    ->from('valor v')
                                                    ->where('v.id_dg = ?', $des->getIdDg())
                                                    ->andwhere('v.mes = ?', $i)
                                                    ->andwhere('v.anio = ?', $afinal)
                                                    ->andwhere('v.id_fo = ?', $fondo->getIdFo());
                                            $valo = $f->fetchOne();
                                            if (!empty($valo)) {
                                                $sumades = $sumades + $valo->getVal();
                                                ?>
                                                <td valign="baseline" align="center"><?php echo $valo->getVal(); ?></td>
                                            <?php } else { ?>
                                                <td valign="baseline" align="center">0.00</td>
                                                <?php
                                            }
                                        }
                                    } else if ($anio > 1) {
                                        for ($i = $minicial; $i <= 12; $i++) {
                                            $f = Doctrine_Query::create()
                                                    ->from('valor v')
                                                    ->where('v.id_dg = ?', $des->getIdDg())
                                                    ->andwhere('v.mes = ?', $i)
                                                    ->andwhere('v.anio = ?', $ainicial)
                                                    ->andwhere('v.id_fo = ?', $fondo->getIdFo());
                                            $valo = $f->fetchOne();
                                            if (!empty($valo)) {
                                                $sumades = $sumades + $valo->getVal();
                                                ?>
                                                <td valign="baseline" align="center"><?php echo $valo->getVal(); ?></td>
                                            <?php } else { ?>
                                                <td valign="baseline" align="center">0.00</td>
                                                <?php
                                            }
                                        }

                                        for ($i = $ainicial + 1; $i <= $afinal - 1; $i++) {
                                            for ($j = 1; $j <= 12; $j++) {
                                                $f = Doctrine_Query::create()
                                                        ->from('valor v')
                                                        ->where('v.id_dg = ?', $des->getIdDg())
                                                        ->andwhere('v.mes = ?', $j)
                                                        ->andwhere('v.anio = ?', $i)
                                                        ->andwhere('v.id_fo = ?', $fondo->getIdFo());
                                                $valo = $f->fetchOne();
                                                if (!empty($valo)) {
                                                    $sumades = $sumades + $valo->getVal();
                                                    ?>
                                                    <td valign="baseline" align="center"><?php echo $valo->getVal(); ?></td>
                                                <?php } else { ?>
                                                    <td valign="baseline" align="center">0.00</td>
                                                    <?php
                                                }
                                            }
                                        }
                                        for ($i = 1; $i <= $mfinal; $i++) {
                                            $f = Doctrine_Query::create()
                                                    ->from('valor v')
                                                    ->where('v.id_dg = ?', $des->getIdDg())
                                                    ->andwhere('v.mes = ?', $i)
                                                    ->andwhere('v.anio = ?', $afinal)
                                                    ->andwhere('v.id_fo = ?', $fondo->getIdFo());
                                            $valo = $f->fetchOne();
                                            if (!empty($valo)) {
                                                $sumades = $sumades + $valo->getVal();
                                                ?>
                                                <td valign="baseline" align="center"><?php echo $valo->getVal(); ?></td>
                                            <?php } else { ?>
                                                <td valign="baseline" align="center">0.00</td>
                                                <?php
                                            }
                                        }
                                    }
                                }
                            }
                            
                            
                            $this->user = sfContext::getInstance()->getUser()->setAttribute('id_tr', $tchos->getIdTp());
                            $this->user = sfContext::getInstance()->getUser()->setAttribute('id_fo', $fondo->getIdFo());
                            ?>
                            <td valign="baseline" align="center"><?php echo $sumades ?></td>
                        </tr>  

                    <?php endforeach; ?>
                    <tr>
                        <td><?php include_partial('tablefooter'); ?></td>
                    </tr>
                    <tr >
                        <td colspan="12"> -- </td>
                    </tr>

                    <?php
                endforeach;
                ?>
                    <tr >
                        <td><?php include_partial('tablefooter2')?></td>
                    </tr>

            </table><br>
        </div>       
    </div><br>
    <div align="center">

          
        <?php 
             $numerofondos = Doctrine_Core::getTable('fondo')->createQuery('f')
                            ->where('f.id_pro = ?', $proyecto->getIdPro())
                            ->execute();
             if(count($numerofondos)==2){?>
                 
             <?php }else{?>

             <?php }
        
        
        ?>
        
    
    
    </div><br><br>
</div>
